package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.adapter.AllocationSingleAdapeter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.AllocationDetailsBean;
import com.txsj.scan.presenter.AllocationListPresenter;
import com.txsj.scan.utils.AppDateMgr;
import com.txsj.scan.utils.NumPointFormat;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.AllocationListIView;
import com.txsj.scan.widget.TextViewFont;

import java.util.ArrayList;
import java.util.List;

/**
 * 库存调拨 调拨单 v层
 */
public class AllocationListActivity extends BaseActivity<AllocationListPresenter> implements AllocationListIView {


    TextView aalaAddNumbers;
    TextView aalaOut0fStorehouse;
    TextView aalaEnterStore;
    TextViewFont tvRukuState;
    RecyclerView rectifyGoods;
    TextView tvRukuCount;
    TextView tvRukuMoney;
    TextView tvTakeUser;
    TextView tvTakeTime;
    TextView tvRemark;
    private int bill_id;
    private LinearLayoutManager linearLayoutManager;
    private AllocationSingleAdapeter adapeter;
    private List<AllocationDetailsBean.DetailsBean> allocationDetailsBean = new ArrayList<>();
    private AllocationDetailsBean data = new AllocationDetailsBean();

    private int total = 0;
    private double totalSum = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allocation_list_act);
        findView();
    }

    @Override
    protected AllocationListPresenter providePresenter() {
        return new AllocationListPresenter(this);
    }

    private void findView() {
        aalaAddNumbers = (TextView) findViewById(R.id.aala_add_numbers);
        aalaOut0fStorehouse = (TextView) findViewById(R.id.aala_out0f_storehouse);
        aalaEnterStore = (TextView) findViewById(R.id.aala_enter_store);
        tvRukuState = (TextViewFont) findViewById(R.id.tv_ruku_state);
        tvRukuCount = (TextView) findViewById(R.id.tv_ruku_count);
        tvRukuMoney = (TextView) findViewById(R.id.tv_ruku_money);
        tvTakeUser = (TextView) findViewById(R.id.tv_take_user);
        tvTakeTime = (TextView) findViewById(R.id.tv_takeTime);
        tvRemark = (TextView) findViewById(R.id.tv_remark);
        rectifyGoods = (RecyclerView) findViewById(R.id.rectify_goods);
        initData();
        bill_id = getIntent().getIntExtra("Bill_id", 0);
        if (bill_id == 0) {
            return;
        }
        presenter.getAllocationListDetails(bill_id);
    }


    protected void initData() {
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rectifyGoods.setLayoutManager(linearLayoutManager);
        adapeter = new AllocationSingleAdapeter(this);
        rectifyGoods.setAdapter(adapeter);
        adapeter.setAllocationSingleListener(new AllocationSingleAdapeter.AllocationSingleListener() {
            @Override
            public void onItemClickListener(int position) {
                Intent intent = new Intent(AllocationListActivity.this, AllocationDetailsActivity.class);
                intent.putExtra("data", data);
                intent.putExtra("gs1_num", data.getDetails().get(position).getGs1_num());
                startActivity(intent);
            }
        });
    }


    //获取调拨单详情 成功
    @Override
    public void getAllocationListDetailsSuccess(AllocationDetailsBean dataBean) {

        data = dataBean;
        aalaAddNumbers.setText(dataBean.getBill_number());
        aalaOut0fStorehouse.setText(dataBean.getOut_warehouse_name());
        aalaEnterStore.setText(dataBean.getInto_warehouse_name());
        tvTakeUser.setText(dataBean.getSponsor_user_name());
        tvTakeTime.setText(AppDateMgr.getDateFormat(dataBean.getBill_time()));
        // 判断备注是否为空
        if (dataBean.getBill_remark() == null || dataBean.getBill_remark().equals("")) {
            tvRemark.setText("无");
        } else {
            tvRemark.setText(dataBean.getBill_remark());
        }

        if (dataBean.getCheck_status() == 7) {
            tvRukuState.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < dataBean.getDetails().size(); i++) {
            total += dataBean.getDetails().get(i).getGoods_count();
            double v = dataBean.getDetails().get(i).getGoods_count() * dataBean.getDetails().get(i).getUnit_price();
            totalSum += v;
        }

        // 向集合去重
        for (int i = 0; i < dataBean.getDetails().size(); i++) {
            boolean isadd = false;
            for (int j = 0; j < allocationDetailsBean.size(); j++) {
                if (dataBean.getDetails().get(i).getGoods_name().equals(allocationDetailsBean.get(j).getGoods_name())) {
                    isadd = true;

                    break;
                }
            }
            if (!isadd) {
                dataBean.getDetails().get(i).getGoods_count();
                allocationDetailsBean.add(dataBean.getDetails().get(i));
            }
        }
        tvRukuCount.setText(total + "");
        tvRukuMoney.setText(NumPointFormat.get2Str(totalSum));
        adapeter.setList(allocationDetailsBean);

    }

    //获取调拨单详情 失败
    @Override
    public void getAllocationListDetailsFail(String s) {

    }

    @Override
    public Context context() {
        return this;
    }
}
